package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.AutoBrightnessParam;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Objects;

/**
 * 获取自动亮度
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetAutoBrightness implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 自动亮度参数
         */
        private AutoBrightnessParam param;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.GetAutoBrightness.InputMsg.Builder inputMsgBuilder =
                CompleteInstruction.GetAutoBrightness.InputMsg.newBuilder();

        final CompleteInstruction.GetAutoBrightness.Builder builder =
                CompleteInstruction.GetAutoBrightness.newBuilder();
        builder.setInput(inputMsgBuilder);
        return builder.build().toByteString();
    }

    @Override
    public GetAutoBrightness byteConvertObject(ByteString bytes) {
        CompleteInstruction.GetAutoBrightness getAutoBrightness = null;
        try {
            getAutoBrightness = CompleteInstruction.GetAutoBrightness.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(getAutoBrightness)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        GetAutoBrightness.OutputMsg outputMsg = new GetAutoBrightness.OutputMsg();
        outputMsg.setParam(new AutoBrightnessParam().formProto(getAutoBrightness.getOutput().getParam()));
        this.setOutputMsg(outputMsg);
        return this;
    }
}

